﻿
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using AMG.Common.Utilities;
using AMG.UserForms.ViewModels;

namespace AMG.UserForms.Service
{
    public class UserFormsAppService
    {
        public void SubmitArchitecturalChangeRequest(ArchitecturalChange architecturalChange)
        {
            //Build email body
            var messageBody = new StringBuilder();
            messageBody.AppendLine();
            messageBody.AppendLine();
            messageBody.AppendLine(string.Format("The following architectural request was submitted on: {0} {1}", 
                architecturalChange.DateSubmitted.ToShortDateString(), architecturalChange.DateSubmitted.ToShortTimeString()));
            messageBody.AppendLine(string.Format("Member Name: {0}", architecturalChange.MemberName));
            messageBody.AppendLine(string.Format("Association Name: {0}", architecturalChange.AssociationName));
            messageBody.AppendLine(string.Format("Address Line 1: {0}", architecturalChange.Address1));
            messageBody.AppendLine(string.Format("Address Line 2: {0}", architecturalChange.Address2));
            messageBody.AppendLine(string.Format("City: {0}", architecturalChange.City));
            messageBody.AppendLine(string.Format("State: {0}", architecturalChange.State));
            messageBody.AppendLine(string.Format("Phone: {0}", architecturalChange.Phone));
            messageBody.AppendLine(string.Format("Email: {0}", architecturalChange.Email));
            messageBody.AppendLine(string.Format("Proposed Change: {0}", architecturalChange.ProposedChange));
            messageBody.AppendLine(string.Format("Description: {0}", architecturalChange.Description));
            messageBody.AppendLine(string.Format("Site Plan Attached: {0}", architecturalChange.SitePlanAttached ? "Yes" : "No"));
            messageBody.AppendLine(string.Format("Elevations Attached: {0}", architecturalChange.ElevationsAttached ? "Yes" : "No"));
            messageBody.AppendLine(string.Format("Color Samples Attached: {0}", architecturalChange.ColorSamplesAttached ? "Yes" : "No"));
            messageBody.AppendLine(string.Format("Extras Attached: {0}", architecturalChange.ExtrasAttached ? "Yes" : "No"));
            messageBody.AppendLine(string.Format("Member Signature: {0}", architecturalChange.MemberSignature));

            var attachments = new List<EmailAttachment>();
            if (architecturalChange.FileList != null && architecturalChange.FileList.Count > 0)
            {
                foreach(var file in architecturalChange.FileList)
                {
                    if (file.FileName.Contains("\\"))
                    {
                        file.FileName = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);
                    }

                    attachments.Add(new EmailAttachment
                                        {
                                            Attachment = file.FileData, AttachmentName = file.FileName
                                        });
                }
            }

            EmailUtilities.SendEmail(messageBody.ToString(), 
                ConfigurationManager.AppSettings["EmailFrom"], 
                ConfigurationManager.AppSettings["EmailTo"], 
                "New Architectural Change Request",
                attachments);
        }
    }
}
